package com.huilian.netty.handler;

import com.huilian.iotbox.data.common.RedisKey;
import com.huilian.netty.constant.CIMConstant;
import com.huilian.netty.model.CIMSession;
import com.huilian.netty.model.SocketMessage;
import com.huilian.netty.repository.SessionRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;


/**
 * 断开连接，清除session
 */
@Component
public class SessionClosedHandler implements CIMRequestHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(SessionClosedHandler.class);
    @Autowired
    private SessionRepository sessionRepository;
    @Override
    public void process(CIMSession ios, SocketMessage message) {
        try {
            LOGGER.info("ios：{}", ios);
            LOGGER.info("message：{}", message);
            Object username = ios.getAttribute(CIMConstant.KEY_USERNANE);
            if(username!=null){
                sessionRepository.remove(String.valueOf(username),ios.getNid());
            }
            LOGGER.info("SessionClosedHandler：{}", message);

        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("离线功能出错: {}", e);
        }
    }

}
